<?php
/**
 * Created by PhpStorm.
 * User: laowang
 * Date: 2018/7/12
 * Time: 下午3:26
 */

namespace app\home\controller;
use app\home\model\WechatUser;
use app\home\model\Write as WriteModel;
use app\home\model\WriteSign;
/*
 * 信息录入   老师端
 */
class Write extends Base
{
    /*
     * 首页
     */
    public function index(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role,school_id')->find();
        if ($User['review_status'] == 0 || $User['role'] == 0 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $Model = new WriteModel();
        $now = $Model->get_list(['teacherid' => $userId,'status' => 0]); // 待审核
        $go = $Model->get_list(['teacherid' => $userId,'status' => ['neq',0]]);  // 已审核
        $this->assign('go',$go);  // 已完成
        $this->assign('now',$now);  // 未完成
        return $this->fetch();
    }
    /**
     * 详情
     */
    public function detail(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role,school_id')->find();
        if ($User['review_status'] == 0 || $User['role'] == 0 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $id = input('id');
        $msg = WriteModel::where('id',$id)->find();
        if ($msg['qqqs']){
            $msg['qqqs'] = json_decode($msg['qqqs']);
        }
        $is_sign = WechatUser::where(['openid' => $msg['openid']])->value('is_sign');
        $this->assign('info',$msg);
        $this->assign('is',$is_sign);  //  是否批阅
        return $this->fetch();
    }
    /**
     * 列表 更多
     */
    public function more(){
        $userId = session('userId');
        $len = input('length');
        $Model = new WriteModel();
        $type = input('type'); // 1 未完成 2 已完成
        if ($type == 1){
            $where = ['teacherid' => $userId,'status' => 0];
        }else{
            $where = ['teacherid' => $userId,'status' => ['neq',0]];
        }
        $res = $Model->get_list($where,$len);
        if (!empty($res)){
            return ['code' => 1,'data' => $res];
        }else{
            return ['code' => 0,'data' => []];
        }
    }
    /**
     * 请假操作
     */
    public function play(){
        $userId = session('userId');
        $id = input('id');
        $res = WriteModel::where(['id'=> $id,'teacherid' => $userId])->update(['status' => 1]);
        if ($res){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }
    /**
     * 批阅页面
     */
    public function paper(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role,school_id')->find();
        $id = input('id');
        $teacherid = WriteModel::where(['id' => $id])->value('teacherid');
        if ($User['review_status'] == 0 || $User['role'] == 0 || $User['subscribe'] == 0 || $teacherid != $userId){
            $this->redirect('Journal/null');
        }
        $this->jssdk();
        $list = WriteSign::where(['wid' => $id,'teacherid' => $teacherid])->order('create_time','desc')->limit(15)->select();
        $this->assign('detail',$list); // 批阅列表
        $this->assign('id',$id);
        return $this->fetch();
    }
    /**
     * 加载更多
     */
    public function moremore(){
        $id = input('id');
        $teacherid = WriteModel::where(['id' => $id])->value('teacherid');
        $len = input('length');
        $list = WriteSign::where(['wid' => $id,'teacherid' => $teacherid])->order('create_time','desc')->limit($len,15)->select();
        foreach ($list as $value){
            $value['time'] = date('Y-m-d H:i',$value['create_time']);
            $value['header'] = get_header($value['teacherid']);
        }
        if ($list){
            return $this->success('加载成功','',$list);
        }else{
            return $this->error('加载失败');
        }
    }
    /**
     * 批阅
     */
    public function sign(){
        $id = input('id');
        $con = input('content');
        $userId = session('userId');
        $Model = new WriteSign();
        $User = WechatUser::where(['openid' => $userId])->field('name,headimgurl')->find();
        $name = $User['name'] ? $User['name'] : '暂无';
        $openid = WriteModel::where('id',$id)->value('openid');
        $res = $Model->save(['teacherid' => $userId,'teacher' => $name,'openid' => $openid,'wid' => $id,'content' => $con,'create_time' => time()]);
        if ($res){
            return $this->success('成功','',['id' => $Model->id,'header'=>$User['headimgurl'],'time' => date('Y-m-d H:i')]);
        }else{
            return $this->error('失败');
        }
    }
    /**
     * 删除评论
     */
    public function dele(){
        $id = input('id');
        if (empty($id)){
            return $this->error('系统参数错误');
        }
        $userId = session('userId');
        $Model = new WriteSign();
        $res = $Model->where(['id' => $id,'teacherid' => $userId])->delete();
        if ($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}